import { defineStore } from 'pinia'
import { ref } from 'vue'

export const usePhotoStore = defineStore('photo', () => {
  const photoList = ref([])
  const currentPhoto = ref(null)
  const currentIndex = ref(0)
  const page = ref(1)
  const hasMore = ref(true)

  // 设置照片列表
  const setPhotoList = (list) => {
    photoList.value = list
  }

  const appendPhotoList = (photos) => {
    photoList.value = [...photoList.value, ...photos]
  }

  // 设置当前照片
  const setCurrentPhoto = (photo) => {
    const allPhotos = photoList.value.filter(item => item.type !== 'folder')
    currentPhoto.value = photo
    currentIndex.value = allPhotos.findIndex(p => p.url === photo.url)
  }

  const setPage = (num) => {
    page.value = num 
  }

  const reset = () => {
    photoList.value = []
    currentPhoto.value = null
    currentIndex.value = 0
    page.value = 1
    hasMore.value = true
  }

  const removePhotos = (urlList) => {
    photoList.value = photoList.value.filter(item => !urlList.some(i => i === item.url))
  }

  return {
    photoList,
    currentPhoto,
    currentIndex,
    page,
    setPage,
    hasMore,
    setPhotoList,
    appendPhotoList,
    setCurrentPhoto,
    removePhotos,
    reset
  }
})